﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;

//------------------------------------------------------------
// shaco Framework
// Copyright © 2017-2021 chang.liu All rights reserved.
// Feedback: 449612236@qq.com
//------------------------------------------------------------

namespace shacoEditor
{
    public partial class InterAssetCreateOrLocationMenu : Editor
    {
        static private string DATA_TYPES_SETTING_PATH { get { return System.IO.Path.Combine(Application.dataPath, "Resources/" + shaco.GameHelper.dateTypesTemplate.GetType().Name + ".asset"); } }
        static private string EXCEL_DATA_SETTING_PATH { get { return System.IO.Path.Combine(Application.dataPath, "Resources/" + shaco.GameHelper.excelSetting.GetType().Name + ".asset"); } }
        static private string SPRITE_ATLAS_SETTING_PATH { get { return shaco.GameHelper.gameConfig.ReadString(shaco.SpriteAtlasSettings.SETTINGS_PATH_KEY); } }

        [MenuItem("shaco/InternalAsset/Local Save Data", false, (int)ToolsGlobalDefine.InternalAssetMenuPriority.LOCAL_SAVE_DATA)]
        static private void ShowDataSaveFile()
        {
            EditorHelper.ShowInFolder(shaco.GameHelper.datasave.savePath);
        }

        [MenuItem("shaco/InternalAsset/DataTypesSetting", false, (int)ToolsGlobalDefine.InternalAssetMenuPriority.DATA_TYPES_SETTING)]
        static private void CreateDataTypesSetting()
        {
            CreateOrLocacationSetting(shaco.GameHelper.dateTypesTemplate.GetType(), DATA_TYPES_SETTING_PATH);
        }

        [MenuItem("shaco/InternalAsset/DataTypesSetting", true, (int)ToolsGlobalDefine.InternalAssetMenuPriority.DATA_TYPES_SETTING)]
        static private bool CreateDataTypesSettingValid()
        {
            return shaco.GameHelper.dateTypesTemplate.GetType().IsInherited<ScriptableObject>();
        }

        [MenuItem("shaco/InternalAsset/ExcelDataSetting", false, (int)ToolsGlobalDefine.InternalAssetMenuPriority.EXCEL_DATA_SETTING)]
        static private void CreateExcelDataSetting()
        {
            CreateOrLocacationSetting(shaco.GameHelper.excelSetting.GetType(), EXCEL_DATA_SETTING_PATH);
        }

        [MenuItem("shaco/InternalAsset/ExcelDataSetting", true, (int)ToolsGlobalDefine.InternalAssetMenuPriority.EXCEL_DATA_SETTING)]
        static private bool CreateExcelDataSettingValid()
        {
            return shaco.GameHelper.excelSetting.GetType().IsInherited<ScriptableObject>();
        }

        [MenuItem("shaco/InternalAsset/SpriteAtlasSetting", false, (int)ToolsGlobalDefine.InternalAssetMenuPriority.SPRITE_ATLAS_SETTING)]
        static private void CreateSpriteAtlasSetting()
        {
            if (System.IO.File.Exists(SPRITE_ATLAS_SETTING_PATH))
            {
                var loadAsset = AssetDatabase.LoadAssetAtPath(SPRITE_ATLAS_SETTING_PATH, shaco.GameHelper.atlasSettings.GetType());
                EditorGUIUtility.PingObject(loadAsset);
                AssetDatabase.OpenAsset(loadAsset);
            }
            else
            {
                SpriteAtlasSettingsWindow.OpenSpriteAtlasSettingsWindow();
            }
        }

        [MenuItem("shaco/InternalAsset/SpriteAtlasSetting", true, (int)ToolsGlobalDefine.InternalAssetMenuPriority.SPRITE_ATLAS_SETTING)]
        static private bool CreateSpriteAtlasSettingValid()
        {
            return shaco.GameHelper.atlasSettings.GetType().IsInherited<ScriptableObject>();
        }

        static private void CreateOrLocacationSetting(System.Type type, string settingPath)
        {
            var loadPath = shaco.GameHelper.gameConfig.ReadString($"InternalAsset.{type.FullName}");
            var loadAsset = shaco.GameHelper.res.LoadExWithDefaultPackage<UnityEngine.Object>(loadPath);
            if (null != loadAsset)
            {
                EditorGUIUtility.PingObject(loadAsset);
                AssetDatabase.OpenAsset(loadAsset);
                return;
            }

            var result = EditorUtility.DisplayDialogComplex("提示", $"没有找到类型{type.FullName}文件路径", "自动创建", "取消", "选择已有文件");
            if (1 == result)
                return;

            if (2 == result)
            {
                var inputPath = string.Empty;
                InputDialogWindow.Open("选择已有文件", () =>
                {
                    GUI.changed = false;
                    inputPath = GUILayoutHelper.PathField(type.Name, inputPath, "*", Application.dataPath);
                    if (GUI.changed)
                    {
                        var checkObj = shaco.GameHelper.res.LoadExWithDefaultPackage<UnityEngine.Object>(inputPath);
                        if (null == checkObj || checkObj.GetType() != type)
                        {
                            Debug.LogError($"路径{inputPath}类型不是{type.FullName}");
                            inputPath = string.Empty;
                        }
                    }
                }, () =>
                {
                    if (string.IsNullOrEmpty(inputPath))
                        return;
                    shaco.GameHelper.gameConfig.WriteString($"InternalAsset.{type.FullName}", inputPath);
                });
                return;
            }

            var assetFolder = System.IO.Path.GetDirectoryName(settingPath);
            if (!System.IO.Directory.Exists(assetFolder))
            {
                System.IO.Directory.CreateDirectory(assetFolder);
                AssetDatabase.ImportAsset(assetFolder);
            }

            var relativeAssetPath = settingPath.Remove(0, Application.dataPath.Length - "Assets".Length);
            var newAsset = ScriptableObject.CreateInstance(type);
            AssetDatabase.CreateAsset(newAsset, relativeAssetPath);
            EditorGUIUtility.PingObject(newAsset);
            AssetDatabase.OpenAsset(newAsset);
        }

        // static private Object LoadSetingAsset(System.Type type, string settingPath)
        // {
        //     var relativePath = settingPath.Remove(0, settingPath.IndexOf("/Resources/") + "/Resources/".Length);
        //     relativePath = relativePath.Remove(relativePath.LastIndexOf('.'));
        //     var retValue = Resources.Load(relativePath, type);
        //     return retValue;
        // }
    }
}